/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__send_data.c                                     */
/*  NOTE      =                                                    */
/*  DATE      = 2014/12/15 by geyh                                 */
/*******************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <CUnit/Basic.h>
#include <CUnit/CUnit.h>
#include <protobuf-c/protobuf-c.h>

#include "../../comm/frcmd_parse_detail.h"
#include "../../comm/client.pb-c.h"
#include "../../comm/frcmd_parse.h"
#include "../../comm/apm.pb-c.h"
#include "../../comm/frcmd_client.h"
#include "../../comm/frcomm.h"
#include "../../comm/frmem.h"
#include "../../comm/frsutl.h"
#include "../../comm/apmtest_cunit.h"

static int __suite_init(void)
{

	return 0;
}

static int __suite_cleanup(void)
{

	return 0;
}

extern int g_i_port;
extern char* g_p_server;

int __receive_data(int i_sockfd, char **pp_receive_data);
int __init_socket(const char *p_addr, unsigned short us_port, int *p_sockfd);
int __send_data(int i_sockfd, char *p_cmd, unsigned int ui_cmd_len);
int __protobuf_do_pack(Command *p_st_cmd, char **pp_data, unsigned int *p_ui_out);

void case1(void)
{
    int i_ret = 0;
    int i_sockfd = 0;
    char *p_data = NULL;
    FILE *fp_file = NULL;
    unsigned int ui_data_size = 0;
    unsigned int ui_used_len = 0;

    fp_file = fopen("test1.txt","r");
    if(fp_file == NULL)
        return;
    fseek(fp_file,0,SEEK_END);
    ui_data_size = ftell(fp_file);
    rewind(fp_file);

    p_data = frmalloc(ui_data_size);
    memset(p_data,0x00,ui_data_size);

    g_i_port = 65535;
    g_p_server = "127.0.0.1";

    i_ret = __init_socket(g_p_server, g_i_port, &i_sockfd);
    if(i_ret != FR_SUCC)
        return;

    ui_used_len = fread(p_data,1,ui_data_size,fp_file);
    if(ui_used_len != ui_data_size)
    {
        frfree(p_data);
        fclose(fp_file);
        return;
    }

    i_ret = __send_data(i_sockfd, p_data, ui_data_size);
    CU_ASSERT_EQUAL(i_ret,FR_SUCC);

    fclose(fp_file);
    frfree(p_data);
    return;

}

int add_suite()
{
	CU_pSuite pSuite = NULL;

	pSuite = CU_add_suite("t__send_data 测试", __suite_init, __suite_cleanup);

	if (NULL == pSuite)
	{
		return 1;
	}


    //增加测试用例
   	CU_add_test(pSuite, "case1", case1);


    return 0;

}


int main(int argc, char **argv)
{
    fr_slab_init(10240,"test");
    run_cunit(add_suite);
    fr_slab_destroy();
    return 0;
}